<?php
namespace app\index\controller;
use think\Db;


class BaiduVideo extends Base
{
    public $exception = [];

 
    public function detail($id){
        // echo '系统维护';die;
        $data = Db::name('baidu_video')->field('doc',true)->where('id',$id)->find();
        if (!empty($data)) {
            Db::name('baidu_video')->where('id',$id)->inc('views')->update();
        }
        if (empty($data['url']) && !empty($data['src'])) {
            $data['url'] = $data['src'];
        }

        $data['is_next'] = Db::name('baidu_video')->where('id','>',$id)->count();
        $data['is_prev'] = Db::name('baidu_video')->where('id','<',$id)->count();

        $tuijians = Db::name('baidu_video')->order(Db::raw('RAND()'))->limit(3)->select();

        return view('detail', [
            'data'=>$data,
            'tuijians'=>$tuijians
        ]);
    }

    public function doc($id){
        // echo '系统维护';die;
        $data = Db::name('baidu_video')->where('id',$id)->find();
        if (!empty($data)) {
            Db::name('baidu_video')->where('id',$id)->inc('views')->update();
        }
        if (empty($data['url']) && !empty($data['src'])) {
            $data['url'] = $data['src'];
        }

        $data['is_next'] = Db::name('baidu_video')->where('id','>',$id)->count();
        $data['is_prev'] = Db::name('baidu_video')->where('id','<',$id)->count();

        $tuijians = Db::name('baidu_video')->order(Db::raw('RAND()'))->limit(3)->select();

        return view('doc', [
            'data'=>$data,
            'tuijians'=>$tuijians
        ]);
    }

    public function next($id){
        // echo '系统维护';die;
        $data = Db::name('baidu_video')->where('id','>',$id)->find();
        if (!empty($data)) {
            Db::name('baidu_video')->where('id',$data['id'])->inc('views')->update();
        }
        if (empty($data['url']) && !empty($data['src'])) {
            $data['url'] = $data['src'];
        }

        $data['is_next'] = Db::name('baidu_video')->where('id','>',$id)->count();
        $data['is_prev'] = Db::name('baidu_video')->where('id','<',$id)->count();

        $tuijians = Db::name('baidu_video')->order(Db::raw('RAND()'))->limit(3)->select();

        return view('detail', [
            'data'=>$data,
            'tuijians'=>$tuijians
        ]);
    }

    public function index(){
        // echo '系统维护';die;
        $map_params = [
            ['key'=>'name','type'=>'like'],
        ];
        $map = getSearchParam($map_params);

        $page_param = [
            'page'=>input('page',1),
            'list_rows'=>input('page_size',100)
        ];
        // 显示筛选
        switch (input('order_mode',0)) {
            case 1:
                $order_mode = 'id';
                $order_type = 'desc';
                break;
            case 2:
                $order_mode = 'views';
                $order_type = 'desc';
                break;
            default:
                $order_mode = 'id';
                $order_type = 'asc';
                break;
        }

        $list = Db::name('baidu_video')->where($map)->order($order_mode,$order_type)->paginate($page_param);
        $page = $list->render();
        $list = $list->toarray();
        foreach ($list['data'] as $k => $vo) {
            $list['data'][$k]['tags'] = explode(',',$vo['tag']);
        }
        return view('index', [
            'list'=>$list,
            'page'=>$page
        ]);
    }

    public function edit(){
        $id = input('id');
        if (empty($id)) { $this->error('id不能为空'); }
        
        $model = $this->model;
        if (request()->isPost()) {
            $fields = [
                'must'=>['name','image','description'],
                'nomust'=>['greet']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
        }

        return parent::updateData($id,$save_data);
    }




   
}
